//
//  SEChatActionBarView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import UIKit

let kChatActionBarOriginalHeight: CGFloat = 50
let kChatActionBarTextViewMaxHeight: CGFloat = 120

protocol SEChatActionBarViewDelegate: NSObjectProtocol {
    func chatActionBarRecordVoiceHideKeyboard()
    /// 显示更多键盘
    func chatActionBarShowMoreKeyboard()
    
    func chatActionBar(_ actionBar: SEChatActionBarView, didClickedVoiceButton button: SEChatButton)
    func chatActionBar(_ actionBar: SEChatActionBarView, didClickedSendButton button: UIButton)
    func chatActionBar(_ actionBar: SEChatActionBarView, didClickedMoreButton button: SEChatButton)
    func chatActionBar(_ actionBar: SEChatActionBarView, didLongTapGesture gesture: UILongPressGestureRecognizer)
}

class SEChatActionBarView: UIView {
    enum ChatKeyboardType: Int {
        case `default`, text, more, record
    }
    
    var keyboardType: ChatKeyboardType = .default
    weak var delegate: SEChatActionBarViewDelegate?
    var inputTextViewCurrentHeight: CGFloat = kChatActionBarOriginalHeight
    
    // MARK: - actions
    
    @IBAction private func voiceButtonAction() {
        delegate?.chatActionBar(self, didClickedVoiceButton: voiceButton)
    }
    
    @IBAction private func moreButtonAction() {
        delegate?.chatActionBar(self, didClickedMoreButton: moreButton)
    }
    
    @IBAction private func sendButtonAction() {
        delegate?.chatActionBar(self, didClickedSendButton: sendButton)
    }
    
    @objc private func longTap(_ gesture: UILongPressGestureRecognizer) {
        delegate?.chatActionBar(self, didLongTapGesture: gesture)
    }
    
    // MARK: - getters
    
    @IBOutlet weak var inputTextView: UITextView! {
        didSet {
            inputTextView.layer.cornerRadius = 5.0
            inputTextView.textContainerInset = UIEdgeInsets.init(top: 7, left: 5, bottom: 5, right: 5)
            inputTextView.returnKeyType = .send
            inputTextView.isHidden = false
            inputTextView.enablesReturnKeyAutomatically = true
            inputTextView.layoutManager.allowsNonContiguousLayout = false
            inputTextView.scrollsToTop = false
            inputTextView.tintColor = Asset.main.color
        }
    }
    
    @IBOutlet private weak var voiceButton: SEChatButton! {
        didSet {
            voiceButton.isHidden = true
            voiceButton.showTextKeyboard = false
        }
    }
    
    @IBOutlet private weak var sendButton: UIButton! {
        didSet {
            sendButton.isIgnoreEvent = false
            sendButton.layer.cornerRadius = sendButton.height / 2
            sendButton.layer.borderWidth = 1
            sendButton.layer.borderColor = Asset.main.color.cgColor
            sendButton.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var moreButton: SEChatButton! {
        didSet {
            moreButton.showTextKeyboard = false
        }
    }
    
    @IBOutlet weak var recordButton: UIButton! {
        didSet {
            recordButton.setBackgroundImage(Asset.chatRecordBg.color.image(), for: .normal)
            recordButton.setBackgroundImage(Asset.chatRecordBg.color.withAlphaComponent(0.8).image(), for: .highlighted)
            recordButton.layer.cornerRadius = 5.0
            recordButton.layer.masksToBounds = true
            recordButton.isHidden = true
            
            let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
            recordButton.addGestureRecognizer(longGesture)
        }
    }
}

extension SEChatActionBarView {
    /// 重置所有 button 图片
    func resetAllButtonUI() {
        self.voiceButton.setImage(Asset.btnChatVoice.image, for: .normal)
        self.voiceButton.setImage(Asset.btnChatVoice.image, for: .highlighted)
    }
    
    /// 唤起键盘
    func inputTextViewCallKeyboard() {
        keyboardType = .text
        inputTextView.isHidden = false
        
        recordButton.isHidden = true
        voiceButton.showTextKeyboard = false
        moreButton.showTextKeyboard = false
    }
    
    /// 显示文本键盘
    func showTextKeyboard() {
        keyboardType = .text
        inputTextView.isHidden = false
        inputTextView.becomeFirstResponder()
        
        recordButton.isHidden = true
        voiceButton.showTextKeyboard = false
        moreButton.showTextKeyboard = false
    }
    
    /// 显示录音
    func showRecording() {
        keyboardType = .default
        inputTextView.isHidden = true
        inputTextView.resignFirstResponder()
        
        delegate?.chatActionBarRecordVoiceHideKeyboard()
        
        recordButton.isHidden = false
        voiceButton.showTextKeyboard = true
        moreButton.showTextKeyboard = false
    }
    
    /// 显示更多键盘
    func showMoreKeyboard() {
        keyboardType = .more
        inputTextView.isHidden = false
        inputTextView.resignFirstResponder()
        
        delegate?.chatActionBarShowMoreKeyboard()
        
        recordButton.isHidden = true
        moreButton.showTextKeyboard = true
    }
    
    /// 取消输入
    func resignKeyboard() {
        keyboardType = .default
        inputTextView.resignFirstResponder()
        moreButton.showTextKeyboard = false
    }
}
